# -*- coding: utf-8 -*-
"""
    批量删除要素文件
"""
import argparse
import arcpy
import os
import sys
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)
from utils.utools import str_to_bool

def batDelGeoFiles(wildcard_str ="tmp_*", workdir=None, delete_features=True, delete_rasters=True, delete_tables=True):
    if wildcard_str is None or wildcard_str == "":
        arcpy.AddError("wildcard cannot be empty!")
        return None
    if workdir is None:
        arcpy.AddError("workdir cannot be None!")
        return None
    arcpy.env.workspace = workdir

    # 在地理数据库（.gdb）中工作时，要查找里面的数据（要素类、表等），请使用 ListFeatureClasses、ListTables，ListRasters 等函数，而不是 ListFiles
    # 创建一个空列表来存放所有匹配的结果
    all_matching_items = []

    fcs = arcpy.ListFeatureClasses(wildcard_str)
    if fcs and delete_features:
        all_matching_items.extend(fcs)

    tables = arcpy.ListTables(wildcard_str)
    if tables and delete_tables:
        all_matching_items.extend(tables)

    rasters = arcpy.ListRasters(wildcard_str)
    if rasters and delete_rasters:
        all_matching_items.extend(rasters)

    try:
        if all_matching_items is not None and len(all_matching_items)!=0:
            for dd in all_matching_items:
                arcpy.Delete_management(dd)
                arcpy.AddMessage("Delete {0} successful!".format(dd))
        else:
            arcpy.AddMessage("没有满足条件的文件!")
    except Exception as e:
        arcpy.AddError("处理过程中发生错误:")
        arcpy.AddError(str(e))
        return None

def main():
    parser = argparse.ArgumentParser(description='批量删除GDB中的要素类【通配符】')
    parser.add_argument('--workdir', required=True, help='工作空间的路径')
    parser.add_argument('--wildcard', required=True, help='通配符字符串')
    parser.add_argument('--delete_features', required=True, help='是否删除要素类')
    parser.add_argument('--delete_rasters', required=True, help='是否删除栅格')
    parser.add_argument('--delete_tables', required=True, help='是否删除表')
    args = parser.parse_args()
    batDelGeoFiles(args.wildcard, args.workdir, str_to_bool(args.delete_features), str_to_bool(args.delete_rasters), str_to_bool(args.delete_tables))

if __name__ == "__main__":
    main()

    # arcgis工具箱模式
    # wildcard_str = arcpy.GetParameterAsText(0)
    # wildcard_str = wildcard_str.strip()
    # batDelFcClass(wildcard_str)

    # 代码模式
    # workdir = r"C:\Users\Luoge7000P\Documents\ArcGIS\Projects\gwlsa_results\gwlsa_results.gdb"
    # wildcard_str = r"wlr_*"
    # batDelFcClass(wildcard_str, workdir)
